package com.cy.server.controller;


import com.cy.server.dto.SalaryDto;
import com.cy.server.pojo.RespBean;
import com.cy.server.pojo.Salary;
import com.cy.server.service.ISalaryService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LiShaoZhuo
 * @since 2021-04-26
 */
@RestController
@RequestMapping("/salary/sob")
public class SalaryController {

    @Autowired
    ISalaryService salaryService;

    @ApiOperation("查询所有账套信息")
    @PostMapping("/list")
    public RespBean selectSalary(@RequestBody SalaryDto salaryDto){
        PageInfo<Salary> pageInfo = salaryService.selectSalary(salaryDto);
        return RespBean.success(pageInfo);
    }

    @ApiOperation("新增账套信息")
    @PostMapping("/add")
    public RespBean addSalary(@RequestBody Salary salary){
        if (salaryService.save(salary)){
            return RespBean.success("新增成功");
        }
        return RespBean.error("新增失败");
    }

    @ApiOperation("更新账套信息")
    @PostMapping("/update")
    public RespBean updateSalary(@RequestBody Salary salary){
        salary.setCreateDate(LocalDateTime.now());
        if (salaryService.updateById(salary)){
            return RespBean.success("更新成功");
        }
        return RespBean.error("更新失败");
    }

    @ApiOperation("删除账套信息")
    @DeleteMapping("/delete/{id}")
    public RespBean deleteSalary(@PathVariable Integer id){
        if (salaryService.removeById(id)){
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }
}
